import store from "../store/index.js";
//服务器地址
// const baseURL = 'https://tcnet.club:3004';
//调试地址
const baseURL = 'https://localhost:3011';
const fileUrl = 'https://tcnet.club:5656';
const FileById = fileUrl + '/TCSoft/Files/GetFileById?id='



export default {
	baseURL,
	FileById,
	config: {
		// 响应拦截器
		handleResponse(res) {
			return new Promise((resolve, reject) => {
				console.log(res);
				let msg = res.data.message || '请求错误';
				if (res.statusCode == 200 || res.data.succeeded) {
					resolve(res.data)
				} else if (res.statusCode == 404) {
					msg = "路径不存在"
					uni.redirectTo({
						url: '/pages/home/home',
						animationType: 'pop-in',
						animationDuration: 200
					})
				} else if (res.statusCode == 401 || res.statusCode == 403) {
					msg = "请重新登录"
					uni.redirectTo({
						url: '/pages/system/sign/sign',
						animationType: 'pop-in',
						animationDuration: 200
					})
				}

				uni.showToast({
					icon: 'none',
					title: msg
				})
				return reject(msg);


			})
		},
		// 请求拦截器
		beforeRequest(url, data, method) {
			return new Promise((resolve, reject) => {
				var options = {
					header: {
						authorization: "Bearer " + store.state.token
					},
					url: baseURL + url,
					method: method,
					data: data
				}
				resolve(options)
			})
		}
	},
	get(url) {
		return this.config.beforeRequest(url, {}, "GET").then(opt => {
			return uni.request(opt)
		}).then(res => this.config.handleResponse(res))
	},
	post(url, data) {
		return this.config.beforeRequest(url, data, "POST").then(opt => {
			return uni.request(opt)
		}).then(res => this.config.handleResponse(res))
	},
	del(url, data){
		return this.config.beforeRequest(url, data, "DELETE").then(opt => {
			return uni.request(opt)
		}).then(res => this.config.handleResponse(res))
	}
}
